﻿using System;
using System.Collections.Generic;
using System.Text;
using IQIGame.Onigao.Framework.Editors;
using JetBrains.Annotations;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEditor.Timeline;
using UnityEngine;
using UnityEngine.Timeline;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [CustomEditor(typeof(ExtendExSignalEmitter))]
    public class ExSignalEmitterInspector : OdinEditor
    {
        public override void OnInspectorGUI()
        {
            target.name = EditorGUILayout.TextField("name:", target.name);
            base.OnInspectorGUI();
        }
    }
    
    [CustomTimelineEditor(typeof(ExtendExSignalEmitter))]
    public class ExSignalEmitterEditor : MarkerEditor
    {
        private StringBuilder tip = new StringBuilder();
        public override MarkerDrawOptions GetMarkerOptions(IMarker marker)
        {
            var options = base.GetMarkerOptions(marker);
            ExtendExSignalEmitter emitter = (ExtendExSignalEmitter)marker;
            var rData = emitter.signals;
            this.tip.Clear();
            for (int i = 0; i < rData.Count; i++)
            {
                if (i != 0)
                {
                    this.tip.Append(" ");
                }
                this.tip.Append($"[{rData[i].type}]");
            }
            var rStr = this.tip.ToString();
            if (string.IsNullOrEmpty(rStr))
            {
                rStr = "空";
            }
            options.tooltip = rStr;
            
            return options;
        }
    }

    public class ExSignalEmitterDataInspector : OdinValueDrawer<ExSignalEmitterData>
    {
        private bool showEffectList = true;
        protected override void DrawPropertyLayout(GUIContent label)
        {
            var entry = this.ValueEntry;
            var property = this.Property;
            var variable = entry.SmartValue;

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField(property.Index.ToString(), EditorBindingUtil.IndexTextStyle, GUILayout.Width(30));

                EditorGUILayout.BeginVertical();
                {
                    variable.type = (EExSignalType)EditorGUILayout.EnumPopup("类型:", variable.type);
                    switch (variable.type)
                    {
                        case EExSignalType.CloseUp:
                            if (variable.paramStr == null || variable.paramStr.Length <= 0)
                            {
                                Array.Resize(ref variable.paramStr, 1);
                            }
                            variable.paramStr[0] = EditorGUILayout.TextField("视频:", variable.paramStr[0]);
                            break;
                        case EExSignalType.SkillEffect:
                            if (variable.paramInt == null || variable.paramInt.Length < 11)
                            {
                                Array.Resize(ref variable.paramInt, 11);
                            }
                            var nStrLen = Math.Max(variable.paramInt.Get(10), 1);
                            if (variable.paramStr == null || variable.paramStr.Length < nStrLen)
                            {
                                Array.Resize(ref variable.paramStr, nStrLen);
                            }
                            ESignalSkillEffectType rType = (ESignalSkillEffectType)variable.paramInt[0];
                            rType = (ESignalSkillEffectType)EditorGUILayout.EnumPopup("效果类型:", rType);
                            variable.paramInt[0] = (int)rType;
                            switch (rType)
                            {
                                case ESignalSkillEffectType.Bullet:
                                    variable.paramInt[10] = Math.Clamp(variable.paramInt.Get(10), 1, 10);
                                    variable.paramInt[10] = EditorGUILayout.IntField("随机特效个数:", variable.paramInt[10]);
                                    this.showEffectList = EditorGUILayout.Foldout(this.showEffectList, "特效");
                                    if (this.showEffectList)
                                    {
                                        for (int i = 0; i < variable.paramInt[10] && variable.paramStr.Length > i; i++)
                                        {
                                            variable.paramStr[i] = EditorGUILayout.TextField($" {i + 1}:", variable.paramStr[i]);
                                        }
                                    }

                                    variable.paramInt[1] = EditorGUILayout.IntField("发射骨骼点:", variable.paramInt[1]);
                                    variable.paramInt[2] = EditorGUILayout.IntField("受击骨骼点:", variable.paramInt[2]);
                                    variable.paramInt[3] = EditorGUILayout.IntField("速度(mm/s):", variable.paramInt[3]);

                                    var rV3Start = new Vector3(variable.paramInt.Get(4) / 100f, variable.paramInt.Get(5) / 100f, variable.paramInt.Get(6) / 100f);
                                    rV3Start = EditorGUILayout.Vector3Field("发射偏移", rV3Start);
                                    variable.paramInt[4] = Mathf.RoundToInt(rV3Start.x * 100);
                                    variable.paramInt[5] = Mathf.RoundToInt(rV3Start.y * 100);
                                    variable.paramInt[6] = Mathf.RoundToInt(rV3Start.z * 100);

                                    var rV3End = new Vector3(variable.paramInt.Get(7) / 100f, variable.paramInt.Get(8) / 100f, variable.paramInt.Get(9) / 100f);
                                    rV3End = EditorGUILayout.Vector3Field("命中偏移", rV3End);
                                    variable.paramInt[7] = Mathf.RoundToInt(rV3End.x * 100);
                                    variable.paramInt[8] = Mathf.RoundToInt(rV3End.y * 100);
                                    variable.paramInt[9] = Mathf.RoundToInt(rV3End.z * 100);
                                    break;
                                case ESignalSkillEffectType.Delay:
                                    variable.paramStr[0] = EditorGUILayout.TextField("特效:", variable.paramStr[0]);
                                    variable.paramInt[1] = EditorGUILayout.IntField("目标骨骼点:", variable.paramInt[1]);
                                    variable.paramInt[2] = EditorGUILayout.IntField("命中延迟时间(ms):", variable.paramInt[2]);
                                    break;
                                case ESignalSkillEffectType.WaitCallback:
                                    variable.paramStr[0] = EditorGUILayout.TextField("等待Key:", variable.paramStr[0]);
                                    break;
                            }

                            break;
                        case EExSignalType.Callback:
                            if (variable.paramStr == null || variable.paramStr.Length <= 0)
                            {
                                Array.Resize(ref variable.paramStr, 1);
                            }
                            variable.paramStr[0] = EditorGUILayout.TextField("Key:", variable.paramStr[0]);
                            break;
                        case EExSignalType.FakeHit:
                            if (variable.paramInt == null || variable.paramInt.Length <= 1)
                            {
                                Array.Resize(ref variable.paramInt, 2);
                            }
                            variable.paramInt[0] = EditorGUILayout.IntField("伤害类型:", variable.paramInt[0]);
                            variable.paramInt[1] = EditorGUILayout.IntField("技能效果index:", variable.paramInt[1]);
                            break;
                    }
                }
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndHorizontal();

            this.ValueEntry.SmartValue = variable;
        }
    }
}